<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!-- <el-form-item label="基本信息ID" prop="basicInfoId">
        <el-input v-model="form.basicInfoId" placeholder="请输入基本信息ID" />
      </el-form-item> -->
      <el-row>
        <el-col :span="8">
          <el-form-item label="刊物类型" prop="publicationType">
            <el-select
              v-model="form.publicationType"
              placeholder="请选择刊物类型"
            >
              <el-option
                v-for="dict in dict.type.publication_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="16">
          <el-form-item label="论文题目" prop="paperTitle">
            <el-input v-model="form.paperTitle" placeholder="请输入论文题目" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="刊物名称" prop="publicationName">
        <el-input
          v-model="form.publicationName"
          placeholder="请输入刊物名称"
          :span="8"
        />
      </el-form-item>
      <el-form-item label="检索地址" prop="searchUrl">
        <el-input
          v-model="form.searchUrl"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  listPapers,
  getPapers,
  delPapers,
  addPapers,
  updatePapers,
} from "@/api/profession/papers";

export default {
  name: "PapersAdd",
  dicts: ["publication_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 论文信息表格数据
      papersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        basicInfoId: null,
        publicationType: null,
        publicationName: null,
        paperTitle: null,
        searchUrl: null,
        coverPdf: null,
        contentPdf: null,
        backCoverPdf: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicInfoId: [
          { required: true, message: "基本信息ID不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询论文信息列表 */
    getList() {
      this.loading = true;
      listPapers(this.queryParams).then((response) => {
        this.papersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        basicInfoId: null,
        publicationType: null,
        publicationName: null,
        paperTitle: null,
        searchUrl: null,
        coverPdf: null,
        contentPdf: null,
        backCoverPdf: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加论文信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPapers(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改论文信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePapers(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPapers(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除论文信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPapers(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "profession/papers/export",
        {
          ...this.queryParams,
        },
        `papers_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
